package dimen;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class DimenTool {

	public static void gen() {
		File file = new File("res/values/dimens.xml");
		BufferedReader reader = null;
		StringBuilder sw480 = new StringBuilder();
		StringBuilder sw600 = new StringBuilder();
		StringBuilder sw720 = new StringBuilder();
		StringBuilder w800 = new StringBuilder();
		StringBuilder w820 = new StringBuilder();
		StringBuilder w960 = new StringBuilder();
		StringBuilder w1280 = new StringBuilder();
		StringBuilder w1920 = new StringBuilder();
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString;
			while ((tempString = reader.readLine()) != null) {
				if (tempString.contains("</dimen>")) {
					String start = tempString.substring(0, tempString.indexOf(">") + 1);
					String end = tempString.substring(tempString.lastIndexOf("<") - 2);
					int num = Integer.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));

					sw480.append(start).append((int) Math.round(num * 0.6)).append(end).append("\n");
					sw600.append(start).append((int) Math.round(num * 0.75)).append(end).append("\n");
					sw720.append(start).append((int) Math.round(num * 0.9)).append(end).append("\n");
					w800.append(tempString).append("\n");
					w820.append(tempString).append("\n");
					w960.append(start).append((int) Math.round(num * 1.2)).append(end).append("\n");
					w1280.append(start).append((int) Math.round(num * 1.6)).append(end).append("\n");
					w1920.append(start).append((int) Math.round(num * 2.4)).append(end).append("\n");
				} else {
					sw480.append(tempString).append("\n");
					sw600.append(tempString).append("\n");
					sw720.append(tempString).append("\n");
					w800.append(tempString).append("\n");
					w820.append(tempString).append("\n");
					w960.append(tempString).append("\n");
					w1280.append(tempString).append("\n");
					w1920.append(tempString).append("\n");
				}
			}
			reader.close();
			System.out.println("<!--  sw480 -->");
			System.out.println(sw480);
			System.out.println("<!--  sw600 -->");
			System.out.println(sw600);

			System.out.println("<!--  sw720 -->");
			System.out.println(sw720);
			System.out.println("<!--  sw800 -->");
			System.out.println(w800);

			String sw480file = "res/values-sw480dp/dimens.xml";
			String sw600file = "res/values-sw600dp/dimens.xml";
			String sw720file = "res/values-sw720dp/dimens.xml";
			String w800file = "res/values-w800dp/dimens.xml";
			String w820file = "res/values-w820dp/dimens.xml";
			String w960file = "res/values-w960dp/dimens.xml";
			String w1280file = "res/values-w1280dp/dimens.xml";
			String w1920file = "res/values-w1920dp/dimens.xml";
			writeFile(sw480file, sw480.toString());
			writeFile(sw600file, sw600.toString());
			writeFile(sw720file, sw720.toString());
			writeFile(w800file, w800.toString());
			writeFile(w820file, w820.toString());
			writeFile(w960file, w960.toString());
			writeFile(w1280file, w1280.toString());
			writeFile(w1920file, w1920.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	public static void writeFile(String file, String text) {
		PrintWriter out = null;
		try {
			out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
			out.println(text);
		} catch (IOException e) {
			e.printStackTrace();
		}

		out.close();
	}

	public static void main(String[] args) {
		gen();
	}
}
